// pages/salesmans/presentBalance/presentBalance.js
var app = getApp();
const pageSize = 20;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_count: 1,
    record_data: [],
    sum: '',//总统计
    salesman_id: '',
    empty: false,
    loadmore: false,
    nomore: false,
    conditions: {
      start_time: '', //开始时间
      end_time: '', //结束时间
    },
    isFilter: false,
    drawer: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options)
    that.setData({
      salesman_id: options.id
    });
    that.accountRecordList();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.accountRecordList();
    }
  },
  reachBottom: function () {
    var that = this;
    var page = that.data.page + 1;
    if (page > that.data.page_count) {
      that.setData({
        nomore: true
      })
      return false;
    }
    that.setData({
      page: page
    })
    that.accountRecordList();
  },
  onPullDownRefresh: function () {
    this.reloadData();
  },
  reloadData() {
    this.setData({
      page: 1,
      record_data: [],
      loadmore: false,
      nomore: false,
    })
    wx.showLoading();
    this.accountRecordList();
  },

  accountRecordList() {
    var that = this;
    app.ajax({
      url: 'Partner/Salesman/accountRecordList',
      method: "POST",
      data: {
        salesman_id: that.data.salesman_id,
        page: that.data.page,
        row: pageSize,
        time_line: false,
        end_time: that.data.conditions.end_time,
        start_time: that.data.conditions.start_time,
        type: 2,
      },
      success: function (res) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code > 0) {
          const source = res.data.data;
          var record = [];
          if(that.data.page==1){
            record = source.data;
          }else{
            record = that.data.record_data.concat(source.data);
          }
          that.setData({
            empty: source.page.total == 0 ? true : false,
            loadmore: source.page.total > record.length ? true : false,
            nomore: source.page.total <= record.length ? true : false,
            record_data: record,
            sum: source.sum,
            page_count: source.page.total_page,
            drawer: false,
          });
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },


  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },

  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete (this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete (this.condition);
    this.reloadData();
  },
})